<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .ani-enter{
      transform: translateX(-100%);
      opacity: 0;
    }
    .ani-enter-active,.ani-leave-active{
      transition: all 500ms;
    }
    .ani-leave-to{
      transform: translateX(100%);
      opacity: 0;
    }
  </style>
</head>

<body>
  <div id="app">
    <div id="content" style="max-width:600px;margin:50px auto">
      <div class="row">
        <div class="col-md-10">
          <input type="text" v-model.trim="inputTxt" class="form-control">
        </div>
        <div class="col-md-2">
          <button @click="addTodo" class="btn btn-primary">增加</button>
        </div>
      </div>
      <div class="list" style="margin-top: 20px;">
        <ul class="list-group">
          <li v-show="!todos.length" class="list-group-item text-center">暂无待办事项</li>
          <transition-group name="ani">
            <li 
              v-for="(todo,index) in todos"
              :key="index"
              class="list-group-item clearfix">
              <span>{{todo.content}} => {{index}}</span>
              <div class="btns pull-right">
                <button 
                  @click="setCompleted(index)"
                  :disabled="todo.isCompleted" class="btn btn-success btn-xs">点击已完成</button>
                <button 
                  @click="delItem(index)"
                  class="btn btn-danger btn-xs">删除</button>
              </div>
            </li>
          </transition-group>
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        inputTxt:'',
        todos: [
          
        ]
      },
      methods: {
        addTodo(){
          // 添加待办事项
          // 为空不添加
          if(this.inputTxt === ''){
            return;
          }
          this.todos.push({
            content: this.inputTxt,
            isCompleted: false
          })
          // 清空文本框
          this.inputTxt = '';
        },
        delItem(index){
          // 删除待办事项
          // splice(index,1)
          if (confirm('您确认删除吗?')) {
            this.todos.splice(index, 1)
          }
        },
        setCompleted(index){
          // 设置已完成
          this.todos[index].isCompleted = true;
        }
      }
    })
  </script>
</body>

</html>